package com.app.utils.task;

import com.app.utils.base.j2se.Common;
import com.app.utils.redis.Redis;
import org.quartz.Job;


/**
 * Description
 * Created by 王伟
 * Date create on 14:15 2019/12/11.
 */
public class Task {

    /**
     * 任务名字 iocBeanName
     */
    private String jobName;

    /**
     * iocBeanName 为空时取 jobName
     */
    private String beanName;

    /**
     * 任务组 按是否多机分为两组
     * <br>single=任务只能单机执行
     * <br>multiple=任务可以多机并发执行
     */
    private String jobGroup;

    /**
     * 任务触发器的cron表达式
     */
    private String cron;

    /**
     * 任务的详细描述 记忆该任务内容
     */
    private String desc;
    /**
     * 服务 app 接口端  task 分析端
     */
    private String server;

    /**
     * 状态 1 关闭 2开启
     */
    private int status;


    /**
     * 任务
     */
    private Job job;

    /**
     * 触发器名称
     * <br>任务的组+任务名+Trigger
     */
    public String triggerName() {
        return jobGroup + jobName + "Trigger";
    }

    /**
     * 验证配置是否正确
     *
     * @return true=配置正确
     */
    public boolean validate() {
        if (Common.isEmpty(jobName)) {
            return false;
        }
        if (Common.isEmpty(jobGroup)) {
            return false;
        }
        if (Common.isEmpty(cron)) {
            return false;
        }
        if (Common.isEmpty(desc)) {
            return false;
        }
        if (!"single".equals(jobGroup) && !"multiple".equals(jobGroup)) {
            return false;
        }
        return true;
    }

    /**
     * 单机情况下 是否可以执行
     *
     * @param day    年月日 20160114
     * @param second 时分秒 190306
     * @return true=可以执行
     */
    public boolean single(final String day, final String second) {
        //累计某天某个任务某秒请求执行次数
        Long num = Redis.hash.hincrBy("TimingTaskSingle" + day, jobName + second, 1, 86400);
        //第一次请求的可以执行
        return null != num && num == 1L;
    }

    public String getJobName() {
        return jobName;
    }

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public String getBeanName() {
        return beanName;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public String getJobGroup() {
        return jobGroup;
    }

    public void setJobGroup(String jobGroup) {
        this.jobGroup = jobGroup;
    }

    public String getCron() {
        return cron;
    }

    public void setCron(String cron) {
        this.cron = cron;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Job getJob() {
        return job;
    }

    public void setJob(Job job) {
        this.job = job;
    }
}
